#include "BasePlane.h"
#include <QPainter>

BasePlane::BasePlane(QWidget *parent)
    : QWidget(parent){}

BasePlane::~BasePlane(){}

void BasePlane::setPixmap(const QPixmap &pixmap)
{
    this->pixmap = pixmap;
    // 根据图片尺寸调整飞机控件大小
    resize(pixmap.size());
    update(); // 触发重绘，显示新图片
}

bool BasePlane::collidesWith(QWidget *other) const
{
    // 获取当前飞机的全局矩形区域
    QRect thisRect = rect().translated(mapToParent(QPoint(0, 0)));
    // 获取目标控件的全局矩形区域
    QRect otherRect = other->rect().translated(other->mapToParent(QPoint(0, 0)));

    // 判断两个矩形是否相交，相交则视为碰撞
    return thisRect.intersects(otherRect);
}

void BasePlane::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    // 在控件左上角绘制飞机图片
    painter.drawPixmap(0, 0, pixmap);
}
